﻿using System;
using System.Collections.Generic;
using System.Text;
using WebServer = SysCommon.Business.CVSS.WS.DCS.WS_DCS_BizCtrlImpl_New;
using Query = SysCommon.Entity.CVSS.WS.DCS.BaseQueryCondition_Ety;
using System.Windows.Forms;
using BindingData = SysCommon.Entity.Business.DCS.View.View_DangerCompany_Ety;
using SaveData = SysCommon.Entity.CVSS.WS.DCS.DangerCompany_Ety;
using CVSS.Client.BDM.Business;

namespace CVSS.Client.BDM.UI
{
    partial class DangerEnterpriseManageFormMDT
    {
        private BindingData[] _bindingData = null;
        private SysCommon.Enums.CVSS.WS.DCS.DBOperatonEnum _operation;

        #region 初始化DataGridView
        private void InitDataGridView()
        {
            try
            {
                string[] en_Cols = new string[] { 
                    "NAME", "PRINCIPAL", "PRINCIPALTEL", 
                    "AREACODE", "IDCARD","COMPANYTYPENAME" ,"ADDRESS"};
                string[] cn_Cols = new string[] { 
                    "企业名称", "负责人姓名", "联系电话", 
                    "所在辖区", "企业证件","企业性质","企业地址" };
                SysCommon.Tools.ShowEntityForDataGridView.SetDefaultDataGrigStyle2(
                    form.dvp_DataView.dg_DataView);
                SysCommon.Tools.ShowEntityForDataGridView.EntityToView(
                    cn_Cols, en_Cols, new BindingData[0], form.dvp_DataView.dg_DataView);
                form.dvp_DataView.Init(0, 0, 0, null, cn_Cols,en_Cols );
                form.dvp_DataView.dg_DataView.AutoSizeColumnsMode =
                     DataGridViewAutoSizeColumnsMode.Fill;
                form.btn_Save.Enabled = false;
                form.btn_Modify.Enabled = false;
                form.btn_Delete.Enabled = false;
                SetControlEnable(false);
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用DangerEnterpriseManageFormMDT.InitDataGridView()出错", ex);
            }
        }
        #endregion


        object dvp_DataView_OnDataChangedHandlerEvent(object sender, SysCommon.UI.DataGridViewForPageCtrl.OnDataChangedHandlerEventArgs e)
        {
            return ShowEntityHelper.SelectionChanged<
                BindingData>(_bindingData, e);
        }

        #region 响应查询事件
        private void BeginQuery()
        {
            try
            {
                WebServer ws = new WebServer();
                Query query = new Query();
                query.KeyWord = form.txt_KeyWord.Text.Trim();
                byte[] bs = ws.QueryDangerCompanyInfoWithCompress(query);
                if (bs != null)
                {
                    BindingData[] ds = SysCommon.Tools.CompressionHelper.DeCompressEntity(bs) as
                        BindingData[];
                    _bindingData = ds;
                    if (_bindingData == null)
                    {
                        _bindingData = new BindingData[0];
                    }
                    ShowEntityHelper.ShowFlowDataInfo<BindingData>(
                        _bindingData, form.dvp_DataView);
                }                
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用DangerEnterpriseManageFormMDT.BeginQuery()出错", ex);
            }
        }
        #endregion

        #region 响应保存操作
        private void Save()
        {
            try
            {
                SaveData data = new SaveData();
                DataGridViewRow row = form.dvp_DataView.dg_DataView.CurrentRow;
                if (row != null)
                {
                    //BindingData bind = form.dvp_DataView.dg_DataView.CurrentRow.DataBoundItem as
                    //    BindingData;
                    //if (bind != null)
                    //{
                    //    data.ID = bind.ID;
                    //}
                    data.ID = (row.Tag as SysCommon.Entity.Business.DCS.View.View_DangerCompany_Ety).ID;

                }
                data.ADDRESS = form.txt_Address.Text.Trim();
                data.AREACODE = form.cmb_AreaName.SelectedValue.ToString();
                data.COMPANYTYPE = Convert.ToInt16(form.cmb_EnterpriseType.SelectedValue.ToString());
                //data.COMPANYTYPE
                data.IDCARD = form.txt_IDCard.Text.Trim();
                data.NAME = form.txt_Name.Text.Trim();
                data.PRINCIPAL = form.txt_Pricipal.Text.Trim();
                data.PRINCIPALTEL = form.txt_PrincipalTel.Text.Trim();

                WebServer ws = new WebServer();
                bool re = ws.DangerCompanyOperation(data, _operation);
                if (!re)
                {
                    MessageBox.Show("保存失败。");
                    return;
                }
                form.btn_Modify.Text = "修改";
                form.btn_Add.Text = "添加";
                form.btn_Save.Enabled = false;
                BeginQuery();
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用Save()出错", ex);
            }
            finally
            {
                form.btn_Add.Enabled = true;
            }
        }
        #endregion

        #region 响应添加事件
        private void Add()
        {
            try
            {
                //判断是否取消添加
                string btnText = form.btn_Add.Text;
                if (btnText.Contains("取消"))  //单击了取消添加按钮
                {
                    //把控件设为不可见
                    SetControlEnable(false);
                    form.btn_Save.Enabled = false;
                    //把按钮的Text设为添加
                    form.btn_Add.Text = "添加";
                    //绑定当前选中的项
                    dg_DataView_SelectionChanged(null, null);
                }
                else  //单击了添加按钮
                {
                    _operation = SysCommon.Enums.CVSS.WS.DCS.DBOperatonEnum.AddRecord;
                    //把控件设为可见
                    SetControlEnable(true);
                    //设置控件的显示文本为空
                    //企业名称
                    form.txt_Name.Text = "";
                    //负责人姓名
                    form.txt_Pricipal.Text = "";
                    //联系电话
                    form.txt_PrincipalTel.Text = "";
                    //所在辖区
                    //form.cmb_AreaName.Text = "";
                    //企业证件编号
                    form.txt_IDCard.Text = "";
                    //企业性质
                    //form.cmb_EnterpriseType.Text = "";
                    //企业地址
                    form.txt_Address.Text = "";
                    //设置按钮的可用性
                    form.btn_Delete.Enabled = false;
                    form.btn_Save.Enabled = true;
                    form.btn_Modify.Enabled = false;
                    form.btn_Add.Text = "取消添加";
                }
            }
            catch (Exception x)
            {
                SysCommon.Global.mainLog.Error("调用DangerCargoManageFormMDT.Add()出错", x);
            }
        }
        #endregion

        #region 响应删除事件
        private void Delete()
        {
            try
            {
                if (MessageBox.Show("确定要删除吗？", "删除", MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question) == DialogResult.No)
                {
                    return;
                }
                //获取当前的选择项
                DataGridViewRow row = form.dvp_DataView.dg_DataView.CurrentRow;
                if (row != null)
                {
                    //BindingData data = row.DataBoundItem as BindingData;
                    //SysCommon.Entity.Business.DCS.View.View_DangerCompany_Ety
                    //if (data != null)
                    //{
                        //调用WebService删除信息
                        SaveData saveData = new SaveData();
                        saveData.ID = (row.Tag as SysCommon.Entity.Business.DCS.View.View_DangerCompany_Ety).ID;
                        WebServer ws = new WebServer();
                        bool re = ws.DangerCompanyOperation(saveData,
                             SysCommon.Enums.CVSS.WS.DCS.DBOperatonEnum.DeleteRecord);
                        if (!re)
                        {
                            MessageBox.Show("保存出错。");
                        }
                        BeginQuery();
                    //}
                }
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用DangerCargoManageFormMDT.Delete()出错", ex);
            }
        }
        #endregion

        #region 响应修改事件
        private void Modify()
        {
            try
            {
                //判断是否取消修改
                string btnText = form.btn_Modify.Text;
                if (btnText.Contains("取消"))  //单击了取消修改按钮
                {
                    //把控件设为不可编辑
                    SetControlEnable(false);
                    //把按钮的Text设为修改
                    form.btn_Modify.Text = "修改";
                    //绑定当前选中的项
                    dg_DataView_SelectionChanged(null, null);
                }
                else
                {
                    _operation = SysCommon.Enums.CVSS.WS.DCS.DBOperatonEnum.ModifyRecord;
                    //把控件设为编辑状态
                    SetControlEnable(true);
                    //把保存按钮设为可用
                    form.btn_Save.Enabled = true;
                    //把添加按钮变为不可用
                    form.btn_Add.Enabled = false;
                    //把删除按钮变为不可用
                    form.btn_Delete.Enabled = false;
                    form.btn_Modify.Text = "取消修改";
                }
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用DangerCargoManageFormMDT.Modify()出错", ex);
            }
        }
        #endregion

        #region 数据绑定
        /// <summary>
        /// 数据绑定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void dg_DataView_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                DataGridViewRow row = form.dvp_DataView.dg_DataView.CurrentRow;
                if (row != null)
                {
                    //检查是否处于编辑状态
                    if (EditorHelper.CheckItem(form.btn_Save,form.btn_Add))
                    {
                        return;
                    }

                    BindingData data = row.Tag as BindingData;
                    
                    if (data != null)
                    {
                        //企业名称
                        form.txt_Name.Text = data.NAME;
                        //负责人姓名
                        form.txt_Pricipal.Text = data.PRINCIPAL;
                        //联系电话
                        form.txt_PrincipalTel.Text = data.PRINCIPALTEL;
                        //所在辖区
                        form.cmb_AreaName.SelectedValue = data.AREACODE;
                        //企业证件编号
                        form.txt_IDCard.Text = data.IDCARD;
                        //企业性质
                        form.cmb_EnterpriseType.SelectedValue = data.COMPANYTYPE;
                        //企业地址
                        form.txt_Address.Text = data.ADDRESS;


                        //设置按钮的可用性
                        form.btn_Delete.Enabled = true;
                        form.btn_Modify.Enabled = true;
                        form.btn_Save.Enabled = false;
                        //设置控件为不可编辑
                        SetControlEnable(false);
                        return;
                    }
                }
                //设置按钮的可用性
                form.btn_Delete.Enabled = true;
                form.btn_Modify.Enabled = true;
                form.btn_Save.Enabled = false;
                form.btn_Add.Enabled = true;
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error(
                    "调用DangerEnterpriseManageFormMDT.dg_DataView_SelectionChanged()出错", ex);
            }
        }
        #endregion

        #region 设置控件的可编辑性
        private void SetControlEnable(bool enable)
        {
            EditorHelper.SetControlEnable(enable,
                new Control[] { form.txt_Name,form.txt_Pricipal,
                        form.txt_PrincipalTel,form.cmb_AreaName,form.txt_IDCard,
                        form.cmb_EnterpriseType,form.txt_Address});
        }

        #endregion


        private void QueryBaseInfo()
        {
            try
            {
                SysCommon.Business.CVSS.WS.BaseData.WS_BaseData_BizCtrlImpl ws =
                    new SysCommon.Business.CVSS.WS.BaseData.WS_BaseData_BizCtrlImpl();
                byte[] bs=ws.GetAllAreaInfo2();
                if (bs != null)
                {
                    SysCommon.Entity.Business.AreaCode_Ety[] ds = SysCommon.Tools.CompressionHelper.DeCompressEntity(bs) as
                        SysCommon.Entity.Business.AreaCode_Ety[];
                    if (ds != null)
                    {
                        form.cmb_AreaName.DataSource = ds;
                        
                        form.cmb_AreaName.DisplayMember = "NAME";
                        form.cmb_AreaName.ValueMember = "CODE";
                        form.cmb_AreaName.DropDownStyle = ComboBoxStyle.DropDownList;
                    }
                }
                CompanyType[] cs = new CompanyType[2];
                cs[0] = new CompanyType();
                cs[0].CODE = 1;
                cs[0].NAME = "购买企业";
                cs[1] = new CompanyType();
                cs[1].NAME = "运输企业";
                cs[1].CODE = 2;
                form.cmb_EnterpriseType.DataSource = cs;
                form.cmb_EnterpriseType.DisplayMember = "NAME";
                form.cmb_EnterpriseType.ValueMember = "CODE";
                form.cmb_EnterpriseType.DropDownStyle = ComboBoxStyle.DropDownList;
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用QueryBaseInfo()出错", ex);
            }
        }

        private class CompanyType
        {
            private int code;
            public int CODE
            {
                get { return code; }
                set { code = value; }
            }

            private string name;
            public string NAME
            {
                get { return name; }
                set { name = value; }
            }
        }
    }
}
